from flask import Blueprint, request, jsonify, render_template, send_from_directory, send_file
from models import DayPaperModel
from exts import db
from utils import JsonResponse
from services import daypaper_service
from datetime import datetime
import json
import re

bp = Blueprint('daypaper', __name__, url_prefix='/daypaper')


@bp.route('/update_daypaper', methods=["GET", "POST"])  # todo: 添加一个设置日期来更新日报，未测试
def update_daypaper():
    if request.method == "POST":
        data = request.form
        since_date = data.get("since_date", None)
        end_date = data.get("end_date", None)
        status, info = daypaper_service.update_daypaper(since_date, end_date)
        if status:
            # return Response("success", "爬取日报成功")
            return JsonResponse.success(info.msg)
        else:
            # return Response("error", "爬取日报失败")
            return JsonResponse.error(info.msg)


@bp.route('/output_excel', methods=["GET", "POST"])
def output_excel():
    if request.method == "GET":
        # return Response('success', '成功打开，请输入起始日期和终止日期来导出excel')
        return JsonResponse.success("GET成功打开")

    since_date = request.form.get("since_date")
    end_date = request.form.get("end_date")
    names = request.form.get("names")
    role = request.form.get("role")

    # 对names做处理
    if names:
        names = names.strip()
        names = tuple(re.split(r"[，,\s]+", names))  # 将名字变为元组
        # names = daypaper_service.fuzzy_search_name(names)  # 进行模糊搜索
        # print(f"查询{names}的日报")  # 查询('姬广壮', '施宇豪')的日报

    # 调用函数
    status, info = daypaper_service.output_daypaper_excel(since_date, end_date, names, role)
    if status:
        # return JsonResponse.success(info.msg, info.data)
        return send_file(info.data, as_attachment=True, download_name='DayPaper.xlsx')
    else:
        return JsonResponse.error(info.msg)


@bp.route('/show', methods=["GET", "POST"])
def show():
    since_date = request.form.get("since_date")
    end_date = request.form.get("end_date")
    names = request.form.get("names")
    role = request.form.get("role")
    em_id = request.form.get("em_id")

    if names:
        names = names.strip()
        names = tuple(re.split(r"[，,\s]+", names))  # 将名字变为元组
        # names = daypaper_service.fuzzy_search_name(names)  # 进行模糊搜索

    status, info = daypaper_service.fetch_daypaper_data(since_date, end_date, names, role, em_id)
    if status:
        return JsonResponse.success(info.msg, info.data)
    else:
        return JsonResponse.error(info.msg)
    # data = json.loads(info.data) if status else None
    # return Response("success", "返回json数据", data)


@bp.route("/particular/<int:em_id>", methods=["GET", "POST"])
def particular(em_id):
    # if request.method == 'GET':
    #     return send_from_directory('static/paper/page/category', 'particular.html')
    # 已知id的情况下
    status, info = daypaper_service.fetch_daypaper_data(em_id=em_id)
    if status:
        return JsonResponse.success(info.msg, info.data)
    else:
        return JsonResponse.error(info.msg)


@bp.route('/submission_rate', methods=["GET", "POST"])
def submission_rate():
    if request.method == 'GET':
        # return Response(200, "GET下打开成功")
        return JsonResponse.success("GET下打开成功")

    # date = request.form.get("date")
    date = request.json.get("date")
    if date:
        date = datetime.strptime(date, "%Y-%m-%d").date()

    status, info = daypaper_service.calculate_submission_rate(date=date)

    # return Response(200, "成功", data)
    if status:
        return JsonResponse.success(info.msg, info.data)
    else:
        return JsonResponse.error(info.msg)


@bp.route('/bar_graph', methods=["GET", "POST"])
def test():
    # daypaper_service.uncommitted_report()
    status, info = daypaper_service.bar_graph()
    return JsonResponse.success(info.msg, info.data)


